package org.credo.net;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * <p>Description:一个简单的可客户端响应处理 </p>
 */
public class ClientWithResponseHandler {

	public final static void main(String[] args){
		HttpClient httpclient = new DefaultHttpClient();
		try {
			HttpGet httpGet = new HttpGet("http://www.baidu.com/");
			System.out.println("执行请求:" + httpGet.getURI());
			// 创建一个响应处理程序,无敌啊,我刚开始写net的Get请求哪有这么方便.
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String responseBody = httpclient.execute(httpGet, responseHandler);
			System.out.println("----------------------------------------");
			System.out.println(responseBody);
			System.out.println("----------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 当不再需要HttpClient实例，关闭连接管理器，以确保立即释放所有系统资源
			httpclient.getConnectionManager().shutdown();
		}
	}

}
